
import java.io.*;

public class NewInputStreamReader extends InputStreamReader
{
	private InputStream in;
	private String enc;
	
	public NewInputStreamReader(InputStream in, String enc)
	{
		super(in);
		this.in = in;
		this.enc = enc;
	}

	public int read() throws IOException
	{
		char ac[] = new char[1];
		if(read(ac, 0, 1) == -1)
			return(-1);
		else
			return(ac[0]);
    }
	
	public int read(char c[], int i, int j) throws IOException
	{
		byte bt[] = new byte[j];
		int num = in.read(bt);
		
		if (num > 0)
		{
			String str;
			try
			{
				str = new String(bt, enc);
				if (!isUnicodeValid(str))
					str = new String(bt, "ISO-8859-1");
				str.getChars(i, j, c, i);
			}
			catch (Exception e)
			{
				str = new String(bt, "ISO-8859-1");
				str.getChars(i, j, c, i);
			}			
		}
		
		return(num);
	}
	
	private boolean isUnicodeValid(String s)
	{
		boolean b = true;
		
		for (int i = 0; i < s.length(); i++)
			if (!Character.isDefined(s.charAt(i)))
				System.out.print(" **** " + s);
			
		return(true);
	}
}